package com.vint.lintcode;


/**
 * 翻转单词
 */
public class Lintcode53 {

	/*
	* @param s: A string
	* @return: A string
	*/
	public String reverseWords(String s) {
		// write your code here
		String tb = reverseString(s);
		StringBuffer sb = new StringBuffer();
		String[] sarray = tb.split(" ");
		boolean first = true;
		for(String s1 : sarray) {
			if(s1.length() == 0){
				continue;
			}
			if(!first) {
				sb.append(' ');
			}else{
				first = false;
			}
			sb.append(reverseString(s1));
		}
		return sb.toString();
	}

	public String reverseString(String s) {
		StringBuffer sb = new StringBuffer();
		for(int i = s.length() - 1; i >= 0 ; i--) {
			sb.append(s.charAt(i));
		}
		return sb.toString();
	}
	public static void main(String[] argv){
		Lintcode53 lintcode53 = new Lintcode53();
		String s = "the sky is blue   ";
		System.out.println(lintcode53.reverseWords(s));
	}

}

